package com.whmnx.my.ui.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableList;

import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.ui.adapter.ViewPagerBindingAdapter2;

import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.tatarka.bindingcollectionadapter2.BindingViewPagerAdapter;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/15 0015 19:48
 * version: 1.0
 * @author Administrator
 */
public class BodouModel extends ToolbarViewModel{
    private String[] titles = new String[]{"兑换播豆包","我的播豆包","历史播豆包"};
    //给ViewPager添加ObservableList
    public ObservableList<ItemViewModel> items = new ObservableArrayList<>();

    //给ViewPager添加ItemBinding
    public ItemBinding<ViewPagerItemBodouViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_viewpager2);

    public BodouModel(@NonNull Application application) {
        super(application);
    }

    public void initToolbar() {
        setTitleText("神奇播豆");
    }

    public void addPage() {
        items.clear();
        //模拟3个ViewPager页面
        for (int i = 1; i <= titles.length; i++) {
            ViewPagerItemBodouViewModel itemViewModel = new ViewPagerItemBodouViewModel(this, i);
            items.add(itemViewModel);
           /* if (i == 1) {
                ViewPagerItemBodouViewModel itemViewModel = new ViewPagerItemBodouViewModel(this, "第" + i + "个页面");
                items.add(itemViewModel);
            }else {
                ViewPagerItemBodouViewModel2 itemViewModel2 = new ViewPagerItemBodouViewModel2(this, "第" + i + "个页面");
                items.add(itemViewModel2);
            }*/
        }
    }


    //给ViewPager添加PageTitle
    public final BindingViewPagerAdapter.PageTitles<ViewPagerItemBodouViewModel> pageTitles = new BindingViewPagerAdapter.PageTitles<ViewPagerItemBodouViewModel>() {
        @Override
        public CharSequence getPageTitle(int position, ViewPagerItemBodouViewModel item) {
            return titles[position];
        }
    };

    //给ViewPager添加Adpter，请使用自定义的Adapter继承BindingViewPagerAdapter，重写onBindBinding方法
    public final ViewPagerBindingAdapter2 adapter = new ViewPagerBindingAdapter2();
    //ViewPager切换监听
    public BindingCommand<Integer> onPageSelectedCommand = new BindingCommand<>(new BindingConsumer<Integer>() {
        @Override
        public void call(Integer index) {
            ToastUtils.showShort("ViewPager切换：" + index);
        }
    });
}


